<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :beforeClose="handleClose"
    :visible.sync="dialogVisible"
    width="450px">
    <el-form :model="contactForm" size="mini" label-width="130px" :rules="rules" ref="contactForm">
      <el-form-item label="联系人：" prop="contactName">
        <el-input v-model="contactForm.contactName" clearable style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="手机号：" prop="contactMobile">
        <el-input v-model="contactForm.contactMobile" clearable style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="职务：">
        <el-select v-model="contactForm.contactDuty" clearable style="width:200px">
          <el-option v-for="(item,index) in supplierDutyList" :key="index" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否为主联系人：">
        <el-radio-group v-model="contactForm.isMainContact">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否有采购决定权：">
        <el-radio-group v-model="contactForm.isBuyPermission">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="产品线：">
        <el-select v-model="contactForm.goodsLineIds" filterable multiple>
          <el-option v-for="(item,index) in goodsLineList" :key="index" :label="item.cddValue" :value="item.cddId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  getCustomerSetting,
} from '@/api/partner';
import {
  supplierContactEdit,
  supplierConstantAPI,
} from '@/api/company';
import { validatePhone } from '@/utils/validate';
const defaultForm = {
  contactId: '',//编辑ID
  supplierId:'',//供应商ID
  contactName:'',//联系人
  contactMobile: '',//手机号
  contactDuty:'',//职务
  isMainContact:null,//是否主联系人
  isBuyPermission: null,//是否有采购决定权
  goodsLineIds:[],//产品线
}
export default {
  name: 'addSupplierContact',
  data() {
    const isvalidatePhone = (rule, value, callback) => {
      if (!validatePhone(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    };
    return {
      title: '',
      dialogVisible: false,
      btnLoading:false,
      contactForm: JSON.parse(JSON.stringify(defaultForm)),
      goodsLineList: [],
      supplierDutyList:[],
      rules: {
        contactName: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }],
        contactMobile: [
          { required: true, message: '请输入联系人手机号', trigger: 'blur' },
          { validator: isvalidatePhone, trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    // 获取供应商配置
    getSupplierConstant(){
      supplierConstantAPI().then((res) =>{
        this.supplierDutyList = res.supplierDuty;
      }).catch(()=>{})
    },
    getCustomerSettingList(){
      getCustomerSetting().then((res) =>{
        res.forEach((el) => {
          if (el.cdtCode == 'goods_line') {//产品线
            this.goodsLineList = el.data
          }
        });
      })
    },
    showPop(type,item) {
      this.dialogVisible = true;
      this.getCustomerSettingList();
      this.getSupplierConstant();
      if (type == 'add') {
        this.title = '新增人员';
        this.contactForm = JSON.parse(JSON.stringify(defaultForm));
        this.contactForm.supplierId = item;
      } else if (type == 'edit') {
        this.title = '编辑人员';
        for (let key in defaultForm) {
          this.contactForm[key] = item[key] ? item[key] : defaultForm[key];
        }
        this.contactForm.isBuyPermission = item.isBuyPermission;
        this.contactForm.isMainContact = item.isMainContact;
        this.contactForm.goodsLineIds = item.goodsLineId.split(',').map(Number);
      }
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.contactForm.resetFields();
    },
    handleConfirm() {
      this.$refs.contactForm.validate((valid) => {
        const data = JSON.parse(JSON.stringify(this.contactForm))
        data.goodsLineIds=data.goodsLineIds.join(',')
        if (valid) {
          this.btnLoading = true;
          supplierContactEdit(data).then((res) => {
            this.$message.success('操作成功');
            this.handleClose();
            this.$emit('update');
            this.btnLoading = false;
          }).catch(() => {
            this.btnLoading = false;
          })
        }
      })
    },
  },
}
</script>
<style scoped>

</style>
